// mock/company.ts

// 模拟延迟
const waitTime = (time: number = 100) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve(true);
        }, time);
    });
};

// 模拟公司数据 - 不包含注册时间
let mockCompanies = Array.from({ length: 100 }, (_, i) => {
    const id = i + 1;
    const status = i % 3 === 0 ? 0 : 1; // 每第三个公司为禁用状态
    return {
        id: id.toString(),
        name: `公司${id.toString().padStart(6, '0')}`,
        code: `CODE_${id.toString().padStart(6, '0')}`,
        address: `上海市浦东新区世纪大道${id * 100}号`,
        status: status, // 1: 启用, 0: 禁用
        status_dictText: status === 1 ? '启用' : '禁用',
        contactName: `联系人${id}`,
        contactPhone: `138${String(i).padStart(8, '0')}`,
    };
});

export default {
    // 获取公司列表
    'GET /sys/company/list': async (req: any, res: any) => {
        await waitTime(500);

        const { current = 1, pageSize = 10, name } = req.query; // 假设按名称搜索

        let filteredCompanies = [...mockCompanies];

        // 应用查询条件 - 按公司名称搜索
        if (name) {
            filteredCompanies = filteredCompanies.filter(company => company.name.includes(name));
        }

        // 分页
        const start = (parseInt(current) - 1) * parseInt(pageSize);
        const end = start + parseInt(pageSize);
        const paginatedData = filteredCompanies.slice(start, end);

        res.json({
            success: true,
            message: '获取成功',
            result: {
                records: paginatedData,
                total: filteredCompanies.length,
            },
        });
    },

    // 删除单个公司
    'DELETE /sys/company/delete': async (req: any, res: any) => {
        await waitTime(300);
        const { id } = req.query;

        const index = mockCompanies.findIndex(c => c.id === id);
        if (index > -1) {
            mockCompanies.splice(index, 1);
            res.json({
                success: true,
                message: '删除成功',
            });
        } else {
            res.status(404).json({
                success: false,
                message: '未找到公司',
            });
        }
    },

    // 添加公司
    'POST /sys/company/add': async (req: any, res: any) => {
        await waitTime(500);
        const data = req.body;

        // 检查编码唯一性 (Mock)
        if (mockCompanies.some(c => c.code === data.code)) {
            res.status(400).json({
                success: false,
                message: '公司编码已存在',
            });
            return;
        }

        const newCompany = {
            ...data,
            id: (mockCompanies.length + 1).toString(), // 简单模拟ID生成
            status: data.status !== undefined ? data.status : 1, // 默认启用
            status_dictText: (data.status !== undefined ? data.status : 1) === 1 ? '启用' : '禁用',
        };
        mockCompanies.push(newCompany);

        res.json({
            success: true,
            message: '新增成功',
            result: newCompany,
        });
    },

    // 编辑公司
    'PUT /sys/company/edit': async (req: any, res: any) => {
        await waitTime(500);
        const data = req.body;
        const index = mockCompanies.findIndex(c => c.id === data.id);

        if (index > -1) {
            // 检查编码唯一性 (排除自己) (Mock)
            if (mockCompanies.some((c, i) => c.code === data.code && i !== index)) {
                res.status(400).json({
                    success: false,
                    message: '公司编码已存在',
                });
                return;
            }

            const updatedCompany = { ...mockCompanies[index], ...data };
            updatedCompany.status_dictText = updatedCompany.status === 1 ? '启用' : '禁用';
            mockCompanies[index] = updatedCompany;

            res.json({
                success: true,
                message: '编辑成功',
                result: updatedCompany,
            });
        } else {
            res.status(404).json({
                success: false,
                message: '未找到公司',
            });
        }
    },
};